import { createSSRApp } from "vue";
import App from "./App.vue";
import axios from "axios";
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

export function createApp() {
  const app = createSSRApp(App);

  // 配置axios
  const http = axios.create({
    baseURL: "http://localhost:8080/api",
    timeout: 5000,
    withCredentials: true, // 允许跨域携带cookie
    headers: {
      'Content-Type': 'application/json'
    }
  });

  // 请求拦截器
  http.interceptors.request.use(
    config => {
      // 在发送请求之前做些什么
      return config;
    },
    error => {
      // 对请求错误做些什么
      return Promise.reject(error);
    }
  );

  // 响应拦截器
  http.interceptors.response.use(
    response => {
      // 对响应数据做点什么
      return response;
    },
    error => {
      // 对响应错误做点什么
      if (error.response) {
        console.error('错误状态码:', error.response.status);
        console.error('错误数据:', error.response.data);
      }
      return Promise.reject(error);
    }
  );

  app.config.globalProperties.$http = http;

  app.use(ElementPlus)

  return {
    app,
  };
}
